package com.ruoyi.bussine.service.common.impl;

import com.ruoyi.bussine.common.enums.user.ProgramTypeEnum;
import com.ruoyi.bussine.domain.dto.wechat.MiniProgramBaseInfoDTO;
import com.ruoyi.bussine.service.common.ModuleConfigService;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhengyz
 */
@Slf4j
@Service
public class ModuleConfigServiceImpl implements ModuleConfigService {

    @Autowired
    private RedisCache redisCache;

    private String getCacheKey(String key) {
        return CacheConstants.SYS_CONFIG_KEY + key;
    }

    @Override
    public String getByKey(String key) {
        String value = redisCache.getCacheObject(getCacheKey(key));
        log.info("get sys_config key=【{}】, value=【{}】", key, value);
        return value;
    }

    @Override
    public String getaMapKey() {
        return getByKey("aMapKey");
    }

    @Override
    public MiniProgramBaseInfoDTO getMiniAppidAndSecret(String programType) {
        if (StringUtils.isBlank(programType)) {
            log.info("小程序类型为空，默认为消费者小程序");
            programType = ProgramTypeEnum.CONSUMER.getValue();
        }
        log.info("获取小程序配置信息={}", programType);
        MiniProgramBaseInfoDTO miniProgramBaseInfoDTO = new MiniProgramBaseInfoDTO();
        miniProgramBaseInfoDTO.setAppid(redisCache.getCacheObject(getCacheKey(programType + "_appId")));
        miniProgramBaseInfoDTO.setSecret(redisCache.getCacheObject(getCacheKey(programType + "_secret")));
        return miniProgramBaseInfoDTO;
    }


    @Override
    public String getWxMchId() {
        return getByKey("wx_pay_mchId");
    }

    @Override
    public String getWxApiV3Key() {
        return getByKey("wx_pay_apiV3Key");
    }

    @Override
    public String getWxMchSerialNo() {
        return getByKey("wx_pay_mchSerialNo");
    }

    @Override
    public String getWxPrivateKeyPath() {
        return getByKey("wx_pay_privateKeyPath");
    }

    @Override
    public String getWxCertificatePath() {
        return getByKey("wx_pay_certificatePath");
    }

    @Override
    public String getWxNotifyUrl() {
        return getByKey("wx_pay_notify_url");
    }
}
